package org.example.jdbc.dao;

import org.example.jdbc.entity.Info;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
@Repository
public class InfoDaoImpl implements InfoDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Override
    public int add(Info info) {
        return jdbcTemplate.update("insert into info (account,password,age,cId,sex)  values (?,?,?,?,?)",info.getAccount(),info.getPassword(),info.getAge(),info.getCId(),info.getSex());
    }

    @Override
    public int del(Integer id) {
        return jdbcTemplate.update("delete from info where id=?",id);
    }

    @Override
    public int update(Info info) {
        return jdbcTemplate.update("update info set account=?,password=?,age=?,cId=?,sex=? where id=?",info.getAccount(),info.getPassword(),info.getAge(),info.getCId(),info.getSex(),info.getId());
    }

    @Override
    public Info get(Integer id) {
        return jdbcTemplate.queryForObject("select * from info where id=?", new BeanPropertyRowMapper<>(Info.class),id);
    }

    @Override
    public List<Info> getAll() {
        return jdbcTemplate.query("select * from info",new BeanPropertyRowMapper<>(Info.class));
    }
}
